<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form toolbar">
        <div class="layui-form-item">
          <div class="layui-inline">
            <button id="purchaseStorageWorkProductBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="purchaseStorageWorkProductBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table class="layui-table" id="purchaseStorageWorkProductTable" lay-filter="purchaseStorageWorkProductTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseStorageWorkProductTableBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseStorageWorkProductForm">
  <form lay-filter="purchaseStorageWorkProductForm" class="layui-form model-form">
    <input name="id" type="hidden" />
    <div class="layui-form-item">
      <label class="layui-form-label">创建者</label>
      <div class="layui-input-block">
        <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">修改者</label>
      <div class="layui-input-block">
        <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">创建人</label>
      <div class="layui-input-block">
        <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">修改人</label>
      <div class="layui-input-block">
        <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">采购入库作业ID</label>
      <div class="layui-input-block">
        <input name="purchaseStorageWorkId" placeholder="请输入采购入库作业ID" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">采购签收物料ID</label>
      <div class="layui-input-block">
        <input name="purchaseSignForProductId" placeholder="请输入采购签收物料ID" type="text" class="layui-input" lay-verify="required" required />
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
      <button class="layui-btn" lay-filter="purchaseStorageWorkProductFormSubmit" lay-submit>保存</button>
    </div>
  </form>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;

    // 渲染表格
    var insTb = table.render({
      elem: "#purchaseStorageWorkProductTable",
      url: config.base + "enterprise/purchase/admin/purchaseStorageWorkProduct/list",
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "checkbox" },
          { field: "creatorName", sort: true, title: "创建人" },
          { field: "modifierName", sort: true, title: "修改人" },
          { field: "purchaseStorageWorkId", sort: true, title: "采购入库作业ID" },
          { field: "purchaseSignForProductId", sort: true, title: "采购签收物料ID" },
          { field: "createTime", sort: true, title: "创建时间" },
          { align: "center", toolbar: "#purchaseStorageWorkProductTableBar", title: "操作", width: 200 }
        ]
      ],
      done: function (res, curr, count) {
        //移除按钮
        var menuButton = config.getMenuButton(location.hash);
        if (menuButton.indexOf("搜索") == -1) {
          $(".layui-form.toolbar").remove();
        }
        if (menuButton.indexOf("修改") == -1) {
          $("[lay-event='edit']").remove();
        }
        if (menuButton.indexOf("删除") == -1) {
          $("[lay-event='del']").remove();
        }
      }
    });

    // 工具条点击事件
    table.on("tool(purchaseStorageWorkProductTable)", function (obj) {
      var data = obj.data;
      if (obj.event === "edit") {
        //修改
        showEditModel(data);
      } else if (obj.event === "del") {
        // 删除
        doDelete(obj);
      }
    });

    //监听排序
    table.on("sort(purchaseStorageWorkProductTable)", function (obj) {
      table.reload("purchaseStorageWorkProductTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type
        }
      });
    });

    // 添加按钮点击事件
    $("#purchaseStorageWorkProductBtnAdd").click(function () {
      showEditModel();
    });

    // 显示编辑弹窗
    function showEditModel(data) {
      admin.open({
        type: 1,
        area: "620px",
        offset: "65px",
        title: data ? "修改采购入库作业物料" : "添加采购入库作业物料",
        content: $("#purchaseStorageWorkProductForm").html(),
        success: function () {
          form.val("purchaseStorageWorkProductForm", data);
          // 表单提交事件
          form.on("submit(purchaseStorageWorkProductFormSubmit)", function (d) {
            layer.load(2);
            admin.req(
              data ? "enterprise/purchase/admin/purchaseStorageWorkProduct/update" : "enterprise/purchase/admin/purchaseStorageWorkProduct/add",
              d.field,
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  table.reload("purchaseStorageWorkProductTable");
                  layer.closeAll("page");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              data ? "PUT" : "POST"
            );
            return false;
          });
        }
      });
    }

    // 删除
    function doDelete(obj) {
      layer.confirm(
        "确定要删除吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin"
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "enterprise/purchase/admin/purchaseStorageWorkProduct/" + obj.data.id,
            {},
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                obj.del();
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "DELETE"
          );
        }
      );
    }

    // 导出excel
    $("#purchaseStorageWorkProductBtnExport").click(function () {
      var checkRows = table.checkStatus("purchaseStorageWorkProductTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要导出的数据", { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, "xls");
      }
    });
  });
</script>
